package models

import "github.com/astaxie/beego/orm"

type PqSlTestItemMaterial struct {
	Id int
	Body string
	PqSlTestLibraryId int
	Ordering int
	QuestionData string
	BaseModel
}

func (this *PqSlTestItemMaterial) ListAll(q ...interface{}) ([]orm.Params,interface{}){
	var nodes []orm.Params
	model := GetSlaveDb()
	model.Raw(`
		select SQL_CALC_FOUND_ROWS ti.*,tl.title as library_title from pq_sl_test_item_material as ti
		inner join pq_sl_test_library as tl on tl.id = ti.pq_sl_test_library_id
		where  ti.pq_sl_test_library_id = ?
		order by ti.ordering asc
	`,q).Values(&nodes)

	var maps []orm.Params
	model.Raw(`select count('ti.*') from pq_sl_test_item as ti
		inner join pq_sl_test_library as tl on tl.id = ti.pq_sl_test_library_id
		where ti.pq_sl_test_library_id = ?`,q[0]).Values(&maps)
	var amount interface{}
	if len(maps) > 0 {
		amount = maps[0]["amount"]
	}
	return nodes,amount
}

func (this *PqSlTestItemMaterial) ListAllQuestion(q ...interface{}) ([]orm.Params){
	var nodes []orm.Params
	model := GetSlaveDb()
	model.Raw(`
		select SQL_CALC_FOUND_ROWS ti.*,tl.title as library_title from pq_sl_test_item as ti
		inner join pq_sl_test_library as tl on tl.id = ti.pq_sl_test_library_id
		where  ti.pq_sl_test_library_id = ? and ti.state > -1
		order by ti.ordering asc
	`,q).Values(&nodes)
	return nodes
}